package com.spark.laboratory.controller;


import com.spark.laboratory.pojo.Comments;
import com.spark.laboratory.pojo.Users;
import com.spark.laboratory.pojo.vo.CommentVO;
import com.spark.laboratory.service.CommentsService;
import com.spark.laboratory.service.UsersService;
import com.spark.laboratory.utils.FengliJsonResult;
import com.spark.laboratory.utils.TESTOKHttp;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *  路由页面
 * @author lixizheng
 */
@Controller
public class PagesController {

    private final UsersService usersService;

    private final CommentsService commentsService;

    @Autowired
    public PagesController(UsersService usersService, CommentsService commentsService) {
        this.usersService = usersService;
        this.commentsService = commentsService;
    }


    @RequestMapping("/")
    public String index(){
        return "index.html";
    }

    @RequestMapping("/index.html")
    public String index_2(Model model) {
//        System.out.println("token = " + token);

//        String dis = "";
//        String un = "";
//        String id = "";
//        try {
//            String result = JWTUtils.dencrty(token);
//            JSONObject jsonObject = new JSONObject(result);
//            dis = jsonObject.get("dis").toString();
//            un = jsonObject.get("un").toString();
//            id = jsonObject.get("id").toString();
//
//            System.out.println(dis);
//            System.out.println(un);
//            System.out.println(id);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

//        List<Comments> comments = commentsService.getCommentsIsNoDelete();
//        int size = usersService.getAllData().size();
//
//        // 做实验人数：有成绩的人数
//        Integer hasScoreUser = usersService.getHasScoreUser();
//        // 实验通过率
//        Double passingRate = usersService.getPassingRate();
//        session.setAttribute("size", comments.size());
//        session.setAttribute("userSize", size);
//        session.setAttribute("hasScoreUser", hasScoreUser);
//        session.setAttribute("passingRate", (int)(passingRate * 100));
//        session.setAttribute("userName", dis);
//        return "/home";
        return "index";
    }

    @ResponseBody
    @RequestMapping("/login2")
    public FengliJsonResult index_(String username, String password, HttpSession session) throws JSONException {

        System.out.println("username = " + username);
        System.out.println("password = " + password);

        JSONObject jsonObject = TESTOKHttp.userCheck(username, password);
        String code = jsonObject.get("code").toString();

        if ("0".equals(code)){
            String name = jsonObject.get("name").toString();
            String username_ = jsonObject.get("username").toString();

            session.setAttribute("userName", name);
            session.setAttribute("username_", username_);
            System.out.println("username_ = " + username_);

            Users u = usersService.queryUser(name);
            if (u == null) {
                Users users = new Users();
                users.setUserName(name);
                users.setCreateDate(new Date());
                users.setIsDelete(1);
                usersService.saveUser(users);
                session.setAttribute("userId", usersService.queryUser(name).getId());
            }else {
                session.setAttribute("userId", u.getId());
            }
            return FengliJsonResult.ok("ok");
        }else {
            String msg = jsonObject.get("msg").toString();

            return FengliJsonResult.errorMsg(msg);
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("userName");
        return "index.html";
    }

    @RequestMapping("/home")
    public String home(HttpSession session){
        List<Comments> comments = commentsService.getCommentsIsNoDelete();
        int size = usersService.getAllData().size();

        // 做实验人数：有成绩的人数
        Integer hasScoreUser = usersService.getHasScoreUser();
        // 实验通过率
        Double passingRate = usersService.getPassingRate();

        session.setAttribute("size", comments.size());
        session.setAttribute("userSize", size);
        session.setAttribute("hasScoreUser", hasScoreUser);
        session.setAttribute("passingRate", (int)(passingRate * 100));

        return "home";
    }

    @RequestMapping("/introduce-viewer.html")
    public String introduceViewerOne(){
        return "introduce-viewer.html";
    }

    @RequestMapping("/introduce-viewer-001.html")
    public String introduceViewer_1(){
        return "introduce-viewer-001.html";
    }
    @RequestMapping("/introduce-viewer-002.html")
    public String introduceViewer_2(){
        return "introduce-viewer-002.html";
    }
    @RequestMapping("/introduce-viewer-003.html")
    public String introduceViewer_3(){
        return "introduce-viewer-003.html";
    }
    @RequestMapping("/introduce-viewer-004.html")
    public String introduceViewer_4(){
        return "introduce-viewer-004.html";
    }
    @RequestMapping("/introduce-viewer-005.html")
    public String introduceViewer_5(){
        return "introduce-viewer-005.html";
    }
    @RequestMapping("/scoring-table.html")
    public String scoringTable_1(){
        return "scoring-table";
    }

    @RequestMapping("/introduce-step.html")
    public String introduceStep(){
        return "introduce-step";
    }
    @RequestMapping("/video.html")
    public String video(){
        return "video";
    }

    @RequestMapping("/evaluate.html")
    public String evaluate(Model model){

        List<Comments> comments = commentsService.getCommentsIsNoDelete();

        List<CommentVO> list = new ArrayList<>();

        for (int i = comments.size() - 1 ; i >= 0 ; i --) {
            Comments comment = comments.get(i);

            CommentVO commentVO = new CommentVO();
            Users user = usersService.getUserById(comment.getUserId());

            commentVO.setCommentContent(comment.getCommentContent());
            //创建SimpleDateFormat对象，指定样式    2019-05-13 22:39:30
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //2019年的第133天。占位符是特定的
            SimpleDateFormat sdf2 = new SimpleDateFormat("y年的第D天");
            //要格式化的Date对象
            Date createDate = comment.getCreateDate();
            //使用format()方法格式化Date对象为字符串，返回字符串
            String format = sdf1.format(createDate);
//            System.out.println(sdf2.format(date));
            commentVO.setCreateDate(format);
            commentVO.setUserName(user.getUserName());

            list.add(commentVO);
        }

        model.addAttribute("list", list);
        return "evaluate";
    }

}
